package com.liu.ssm.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.liu.ssm.controller.service.StudentService;
import com.liu.ssm.controller.service.UserService;
import com.liu.ssm.entity.Student;
import com.liu.ssm.entity.User;
import com.liu.ssm.util.CpachaUtil;

/**
 * 系统主页控制器
 * @author SHIYI
 *
 */
@RequestMapping("system")
@Controller
public class SystemController {
	@Autowired
	private UserService userservice;
	
	@Autowired
	private StudentService studentservice;
	
	@RequestMapping("holleworld")
    public String holleWord() {//跳转系统主页面
		return "system/index"; 
    }
	
	/**
	 * 登入页面
	 * @return
	 */
	@RequestMapping(value = "login",method = RequestMethod.GET)
     public String login() {//跳转系统主页面
		return "system/login"; 
     }
	
	

	/**
	 * 注销登录 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/login_out",method=RequestMethod.GET)
	public String loginOut(HttpServletRequest request){
		request.getSession().setAttribute("user", null);
		return "redirect:login";
	}
	
	
	/**
	 * 登入表单提交
	 * @return
	 */
	@RequestMapping(value = "login",method = RequestMethod.POST)
	@ResponseBody
    public Map<String,String> login(@RequestParam(value = "username" ,required = true) String username,
    		@RequestParam(value = "password" ,required = true) String password,
    		@RequestParam(value = "vcode" ,required = true) String vcode,
    		@RequestParam(value = "type" ,required = true) int type,
    		 HttpServletRequest request)
    	{
		   Map<String,String> map = new HashMap<String, String>();
		   if(StringUtils.isEmpty(username)) {
			   map.put("type", "error");
			   map.put("mas", "用户名不能够为空！");
			   return map;
		   }
		   if(StringUtils.isEmpty(password)) {
			   map.put("type", "error");
			   map.put("mas", "密码不能够为空！");
			   return map;
		   }
		   if(StringUtils.isEmpty(vcode)) {
			   map.put("type", "error");
			   map.put("mas", "验证码不能够为空！");
			   return map;
		   }
		   String loginCpacha = (String)request.getSession().getAttribute("loginCpacha");
		   if(StringUtils.isEmpty(loginCpacha)) {
			   map.put("type", "error");
			   map.put("mas", "长时间未操作，会话超时，请刷新页面重试！");
			   return map;
		   }
		   if(!vcode.toUpperCase().equals(loginCpacha.toUpperCase())) {
			   map.put("type", "error");
			   map.put("mas", "验证码错误！");
			   return map;
		   }
		   request.getSession().setAttribute("loginCpacha",null);
		   //数据库查询用户
		    if(type == 1) {//管理员
		          User user = userservice.findByUsername(username);
		          if(user == null) {
			            map.put("type", "error");
			            map.put("mas", "用户不存在");
			            return map;
		            }
		          if(!password.equals(user.getPassword())) {
			            map.put("type", "error");
			            map.put("mas", "密码错误！");
			            return map;
		           }
		         request.getSession().setAttribute("user", user);
		   }
		    if(type == 2) {//学生
		    	Student student = studentservice.findByUserName(username);
		    	if(student == null) {
		            map.put("type", "error");
		            map.put("mas", "学生不存在");
		            return map;
	            }
	          if(!password.equals(student.getPassword())) {
		            map.put("type", "error");
		            map.put("mas", "密码错误！");
		            return map;
	           }
	          request.getSession().setAttribute("user", student);
		   }
		    request.getSession().setAttribute("userType", type);
		   map.put("type", "success");
		   map.put("mas", "登入成功");
		   return map;
		 
    }
	
	
	/**
	 * 登入验证码
	 * @param response
	 * @param vl
	 * @param w
	 * @param h
	 * @param request
	 */
	@RequestMapping("get_cpacha")
    public void  getcpacha(HttpServletResponse response,
    		    @RequestParam(value = "vl",defaultValue ="4",required = false) int vl,
    		    @RequestParam(value = "w",defaultValue ="98",required = false) int w,
    		    @RequestParam(value = "h",defaultValue ="33",required = false) int h,
    		    HttpServletRequest request) 
       {
		CpachaUtil cpach = new CpachaUtil(vl,w,h);//设置验证码个数，画布大小
		String vCode = cpach.generatorVCode();//获取验证码的字符
		request.getSession().setAttribute("loginCpacha", vCode);
		BufferedImage rotateVCodeImage = cpach.generatorRotateVCodeImage(vCode, true);//放入画布，生成验证码
		try {
			ImageIO.write(rotateVCodeImage, "gif", response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
		
    }
	
	
}
